import api from '../../api'
import tipBox from "../../plugins";

const state = {
    loops: [],
    benefits: [],
    mainTypes: [],
    hotTypes: [],
    likes: []
}
const actions = {
    loadHomeDatas({ commit }, arg) {
        // console.log(store,arg)
        // 发生请s求
        api.loadDataByPids({
            appPlat: "m",
            pids: "136556,51833,106930,51836"
        }).then(({ success, data }) => {
            if (!success) {
                return Promise.reject("请求失败");
            }
            commit("setloops", data[136556].list);
            commit("setbenefits", data[51833].list);
            commit("setmainTypes", data[106930].list);
            commit("sethotTypes", data[51836].list);
        }).catch((error) => {
            // console.log(error);
            tipBox.showMsg("数据加载失败","error",2000);
        })
    },
    loadHomeLikes({ commit }) {
        api.loadProductsByPid({
            pid: "9750",
            sort: "pop",
            cKey: 46
        }).then(({ success, result: { wall: { docs } } }) => {
            if (!success) {
                return Promise.reject("请求失败");
            }
            // this.homeData.likes = docs;
            commit("setlikes", docs)
        }).catch(error => {
            // console.log("请求失败", error);
            tipBox.showMsg("数据加载失败","error",2000);
        });
    }
}
const mutations = {
    setloops(state, nv) {
        state.loops = nv;
    },
    setbenefits(state, nv) {
        state.benefits = nv;
    },
    setmainTypes(state, nv) {
        state.mainTypes = nv;
    },
    sethotTypes(state, nv) {
        state.hotTypes = nv;
    },
    setlikes(state, nv) {
        state.likes = nv;
    }
}

export default {
    namespaced: true, // 开启命名空间
    state,
    actions,
    mutations
}